//
//  PriceQueryChooseViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/9.
//  Copyright (c) 2015年 nest. All rights reserved.

#import "PriceQueryChooseViewController.h"
#import "ZHPickView.h"

@interface PriceQueryChooseViewController () <UITableViewDataSource, UITableViewDelegate, ZHPickViewDelegate, UITextFieldDelegate>

@property (strong, nonatomic) NSArray *detailArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic)NSString *index;
@property(nonatomic,strong)ZHPickView *pickview;

@end

@implementation PriceQueryChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_ifChooseWay) {
        self.detailArray = @[@"自提",@"送货上门",@"物流到付",@"快递到付"];
    } else {
        NSArray *array = @[@[@[],@[@"是",@"否"],@[@"包工包料",@"清加工",@"我指定面辅料供应 商",@"其他"],@[]],
                           @[@[@"我提供",@"不需要",@"委托采购"],@[@"我提供",@"不需要",@"委托采购"],@[],@[],@[],@[@"是",@"否"]],
                           @[@[],@[]]];
        self.detailArray = [array objectAtIndex:[self.positionArray[0] integerValue]];
        self.detailArray = [self.detailArray objectAtIndex:[self.positionArray[1] integerValue]];
    }
    NSString *title = self.positionArray[2];
    self.title = title;
    self.textField.delegate = self;
    
    for(int i=0; i<self.detailArray.count; i++) {
        if ([_detailText isEqualToString:[self.detailArray objectAtIndex:i]]) {
            _index = [NSString stringWithFormat:@"%d", i];
            return;
        }
    }
    
    self.textField.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
    self.textField.layer.borderWidth = 1;
    self.textView.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
    self.textView.layer.borderWidth = 1;
    [UIUtil configTextField:_textField withTitle:@""];
    _textField.text = _detailText;
    _textView.text = _detailText;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIUtil colorWithHexString:appFontColor]}];
    if ([_choose isEqualToString:@"0"]) {
        self.tableView.hidden = YES;
        self.textView.hidden = YES;
        self.textField.hidden = NO;
    } else if ([_choose isEqualToString:@"1"]) {
        self.tableView.hidden = YES;
        self.textView.hidden = NO;
        self.textField.hidden = YES;
    } else {
        self.tableView.hidden = NO;
        self.textView.hidden = YES;
        self.textField.hidden = YES;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([_choose isEqualToString:@"0"]) {
        if (_chooseBlock) {
            _chooseBlock(_textField.text);
        }
    } else if ([_choose isEqualToString:@"1"]) {
        if (_chooseBlock) {
            _chooseBlock(_textView.text);
        }
    }
    [self.pickview remove];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _detailArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *chooseCellIdentifier = @"chooseCell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:chooseCellIdentifier];
    [cell.textLabel setTextColor:[UIUtil colorWithHexString:appFontColor]];
    cell.textLabel.text = [_detailArray objectAtIndex:indexPath.row];
    if ([_index isEqualToString:[NSString stringWithFormat:@"%ld", (long)indexPath.row]]) {
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"check"]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _index = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
    [self.tableView reloadData];
    if (_chooseBlock) {
        _chooseBlock([self.tableView cellForRowAtIndexPath:indexPath].textLabel.text);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([self.title isEqualToString:@"预计大货下单时间"] || [self.title isEqualToString:@"要求样衣交付时间"] || self.ifChooseDate) {
        [self.pickview remove];
        NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
        self.pickview=[[ZHPickView alloc] initDatePickWithDate:date datePickerMode:UIDatePickerModeDate isHaveNavControler:NO];
        self.pickview.delegate = self;
        [self.pickview show];
        return NO;
    }
    return YES;
}

#pragma mark - ZhpickVIewDelegate
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString {
    [self.view endEditing:YES];
    self.textField.text = [resultString substringToIndex:10];
    if (_chooseBlock) {
        _chooseBlock(self.textField.text);
    }
}

@end
